package cn.iocoder.yudao.module.fengdong.smartelderly.service.dayconfig;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.dayconfig.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.dayconfig.DayconfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.dayconfig.DayconfigConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.dayconfig.DayconfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder.getTenantId;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 月缴费配置 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class DayconfigServiceImpl implements DayconfigService {

    @Resource
    private DayconfigMapper dayconfigMapper;

    @Override
    public Long createDayconfig(DayconfigCreateReqVO createReqVO) {

        validateTenantIdxists(createReqVO);
        // 插入
        DayconfigDO dayconfig = DayconfigConvert.INSTANCE.convert(createReqVO);
        dayconfigMapper.insert(dayconfig);
        // 返回
        return dayconfig.getId();
    }

    @Override
    public void updateDayconfig(DayconfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateDayconfigExists(updateReqVO.getId());
        // 更新
        DayconfigDO updateObj = DayconfigConvert.INSTANCE.convert(updateReqVO);
        dayconfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteDayconfig(Long id) {
        // 校验存在
        validateDayconfigExists(id);
        // 删除
        dayconfigMapper.deleteById(id);
    }

    private void validateTenantIdxists(DayconfigCreateReqVO createReqVO) {
        if (dayconfigMapper.selectTenantIdOne(createReqVO) >0 ) {
            throw exception(DAYCONFIG_TENANTID_EXISTS);
        }
    }
    private void validateDayconfigExists(Long id) {
        if (dayconfigMapper.selectById(id) == null) {
            throw exception(DAYCONFIG_NOT_EXISTS);
        }
    }

    @Override
    public DayconfigDO getDayconfig(Long id) {
        return dayconfigMapper.selectById(id);
    }

    @Override
    public List<DayconfigDO> getDayconfigList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return dayconfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DayconfigDO> getDayconfigPage(DayconfigPageReqVO pageReqVO) {
        return dayconfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DayconfigDO> getDayconfigList(DayconfigExportReqVO exportReqVO) {
        return dayconfigMapper.selectList(exportReqVO);
    }

}
